<?php

namespace App\Levels;

use App\Levels\Points\PointInterface;
use App\Models\Order;

class BaseLevel
{
    /**
     * @var PointInterface[]
     */
    protected $points = [];

    /**
     * ["A" => [10.5,],]
     */
    protected $levelPercents = [
        "A" => [],
        "B" => [],
        "C" => [],
    ];

    /**
     * ["A" => [[10, 20],], ]
     */
    protected $levelScope = [
        "A" => [],
        "B" => [],
        "C" => [],
    ];

    public function __construct()
    {
        $count = count($this->points);
        $value = 0;
        $this->initPercents($value, $count);
    }

    /**
     * ["A" => ["全店营业额累计" => 10.5,],]
     */
    public function getPoints()
    {
        $res = ["A" => [], "B" => [], "C" => []];
        $labels = $this->getLabels();

        foreach ($this->levelPercents as $k => $v) {
            //已labels为key，v为value
            $res[$k] = array_combine($labels, $v);
        }

        return $res;
    }

    /**
     * ["A" => ["全店营业额累计" => 10.5,],]
     * @param $points
     * @return void
     */
    public function setPoints($points)
    {
        $labels = $this->getLabels();

        foreach ($points as $k => $v) {
            //将$v数组按照labels的顺序排列
            $this->levelPercents[$k] = array_values(array_merge(array_flip($labels), $v));
        }
    }

    /**
     * ["A" => ["全店营业额累计" => [10, 20],],]
     */
    public function getScopes()
    {
        $res = ["A" => [], "B" => [], "C" => []];
        $labels = $this->getLabels();
        foreach ($this->levelScope as $k => $v) {
            $res[$k] = array_combine($labels, $v);
        }
        return $res;
    }

    /**
     * ["A" => ["全店营业额累计" => [10, 20],],]
     * @param $scopes
     * @return void
     */
    public function setScopes($scopes)
    {
        $labels = $this->getLabels();
        foreach ($scopes as $k => $v) {
            $this->levelScope[$k] = array_values(array_merge(array_flip($labels), $v));
        }
    }

    private function getLabels()
    {
        $res = [];
        foreach ($this->points as $point) {
            $res[] = $point->getLabel();
        }

        return $res;
    }

    //计算订单积分
    public function calculateOrder($level, Order $order)
    {
        $percents = $this->levelPercents[$level];
        $total = 0;
        foreach ($this->points as $k => $point) {
            $point->setPercent($percents[$k]);
            $total += $point->getFromOrder($order);
        }

        return $total;
    }

    protected function initPercents($value, $count)
    {
        //生成count个value
        foreach ($this->levelPercents as $k => $v) {
            $this->levelPercents[$k] = array_fill(0, $count, $value);
        }
    }

    protected function initScope($label, $min, $max)
    {
        foreach ($this->levelScope as $k => $v) {
            $this->levelScope[$k][$label] = [$min, $max];
        }
    }

    //计算评价积分
    public function calculateComment($level, int $evaluation)
    {
        //TODO:计算评价积分
    }
}
